package io;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Server {

	public static void main(String[] args) throws IOException {
		 // 获取服务器端通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        // 绑定端口号
        serverSocketChannel.bind(new InetSocketAddress(8888));

        // 接收客户端通道
        SocketChannel socketChannel = serverSocketChannel.accept();

        // 分配缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        // 每次读取数据的长度
        int len;

        // 接收数据
        while ((len = socketChannel.read(buffer)) > 0) {

            buffer.flip();

            // 将每次读取的数据输出
            System.out.println(new String(buffer.array(), 0, len));

            buffer.clear();
        }
	}
}
